package com.jjwxc.jwjskandriod.activity;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.framework.widget.MyDialog;
import com.jjwxc.jwjskandriod.model.LoginResponse;
import com.jjwxc.jwjskandriod.my.data.UserInfoBg;
import com.jjwxc.jwjskandriod.readActivity.local.ReadSettingManager;
import com.jjwxc.jwjskandriod.readActivity.utils.Constant;
import com.jjwxc.jwjskandriod.readActivity.utils.FileUtils;
import com.jjwxc.jwjskandriod.readActivity.utils.SharedPreUtils;
import com.jjwxc.jwjskandriod.readActivity.utils.UMConstant;
import com.jjwxc.jwjskandriod.util.PhoneInfoUtils;
import com.umeng.analytics.MobclickAgent;

import androidx.constraintlayout.widget.ConstraintLayout;

public class SettingActivity extends BaseActivity implements View.OnClickListener {

    private ConstraintLayout cl_security;
    private ConstraintLayout cl_about;
    private ConstraintLayout cl_account;
    private ConstraintLayout cl_adolescent;
    private ConstraintLayout cl_qinghuancun;
    private ConstraintLayout cl_yinsizhengce;
    private ConstraintLayout cl_kefu;
    private ConstraintLayout cl_yssz;
    private ConstraintLayout cl_grxx;
    private ConstraintLayout cl_disanfang;
    private TextView tv_tuichu;
    private TextView tv_adolescent;
    private TextView tv_cache;

    @Override
    public int getContentViewId() {
        return R.layout.activity_settring;
    }

    @Override
    public void findView() {
        setTitle("设置");
        cl_security = findViewById(R.id.cl_security);
        cl_about = findViewById(R.id.cl_about);
        cl_account = findViewById(R.id.cl_account);
        cl_adolescent = findViewById(R.id.cl_adolescent);
        tv_tuichu = findViewById(R.id.tv_tuichu);
        cl_qinghuancun = findViewById(R.id.cl_qinghuancun);
        cl_yinsizhengce = findViewById(R.id.cl_yinsizhengce);
        cl_kefu = findViewById(R.id.cl_kefu);
        cl_yssz = findViewById(R.id.cl_yssz);
        cl_disanfang = findViewById(R.id.cl_disanfang);
        cl_grxx = findViewById(R.id.cl_grxx);
        tv_adolescent = findViewById(R.id.tv_adolescent);
        tv_cache = findViewById(R.id.tv_cache);
    }

    @Override
    public void setListener() {
        cl_security.setOnClickListener(this);
        cl_about.setOnClickListener(this);
        cl_adolescent.setOnClickListener(this);
        tv_tuichu.setOnClickListener(this);
        cl_qinghuancun.setOnClickListener(this);
        cl_yinsizhengce.setOnClickListener(this);
        cl_kefu.setOnClickListener(this);
        cl_yssz.setOnClickListener(this);
        cl_disanfang.setOnClickListener(this);
        cl_grxx.setOnClickListener(this);
        cl_account.setOnClickListener(this);
    }

    @Override
    public void afterCreate() {
        if (!PreUtils.getBoolean("QING_switch", false)) {
            tv_adolescent.setText("开启青少年模式");
        } else {
            tv_adolescent.setText("关闭青少年模式");
        }
        tv_cache.setText(FileUtils.getDirOrFileSize(Constant.BOOK_CACHE_PATH));
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onEvent(this, UMConstant.SET_UP_ANDROID);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.cl_security) {
            Intent intent = new Intent(SettingActivity.this, SecurityActivity.class);
            startActivity(intent);
            MobclickAgent.onEvent(SettingActivity.this,UMConstant.SET_UP_ANDROID_ACCOUNT_SECURITY_CLICK);
        } else if (v.getId() == R.id.cl_about) {
            Intent intent = new Intent(SettingActivity.this, AboutActivity.class);
            startActivity(intent);
            MobclickAgent.onEvent(SettingActivity.this,UMConstant.SET_UP_ANDROID_ACCOUNT_ABOUT_ME_CLICK);
        } else if (v.getId() == R.id.cl_account) {
            Intent intent = new Intent(SettingActivity.this, AccountDelActivity.class);
            startActivity(intent);
            MobclickAgent.onEvent(SettingActivity.this,UMConstant.SET_UP_ANDROID_ACCOUNT_ACCOUNT_CANCELLATION_CLICK);
        } else if (v.getId() == R.id.cl_adolescent) {
            Intent intent = new Intent(SettingActivity.this, AdolescentActivity.class);
            startActivity(intent);
            MobclickAgent.onEvent(SettingActivity.this,UMConstant.SET_UP_ANDROID_ACCOUNT_OPEN_YOUNG_ANDROID_CLICK);
        } else if (v.getId() == R.id.cl_yinsizhengce) {
            Intent intent = new Intent(SettingActivity.this, WebActivity.class);
            intent.putExtra("WEB_URL", Url.privacy);
            intent.putExtra("WEB_NAME","隐私权政策");
            startActivity(intent);
            MobclickAgent.onEvent(SettingActivity.this,UMConstant.SET_UP_ANDROID_PRIVACY_POLICY_CLICK);
        } else if(v.getId() == R.id.cl_kefu){
            Intent intent = new Intent(SettingActivity.this, WebActivity.class);
            intent.putExtra("WEB_URL", Url.backend);
            intent.putExtra("WEB_NAME","问题反馈");
            startActivity(intent);
            MobclickAgent.onEvent(SettingActivity.this,UMConstant.SET_UP_ANDROID_ACCOUNT_QUESTION_CLICK);
        }else if(v.getId() == R.id.cl_yssz){
            Intent intent = new Intent(SettingActivity.this,PrivacyActivity.class);
            startActivity(intent);
            MobclickAgent.onEvent(SettingActivity.this,UMConstant.SET_UP_ANDROID_PRIVACY_SET_CLICK);
        }else if(v.getId() == R.id.cl_disanfang){
            Intent intent = new Intent(SettingActivity.this, WebActivity.class);
            intent.putExtra("WEB_URL", Url.androidsdk);
            intent.putExtra("WEB_NAME","第三方SDK目录");
            startActivity(intent);
        }else if(v.getId() == R.id.cl_grxx){
            Intent intent = new Intent(SettingActivity.this, WebActivity.class);
            intent.putExtra("WEB_URL", Url.personalInfo);
            intent.putExtra("WEB_NAME","个人信息获取清单");
            startActivity(intent);
        }

        else if (v.getId() == R.id.tv_tuichu) {
            new MyDialog(SettingActivity.this, "你确定要退出登录吗？", "确定", "取消", new MyDialog.DialogClickListener() {
                @Override
                public void onOkClick(Dialog dialog) {
                    touristLogin();

                    /*
                    FileUtils.deleteFile(Constant.BOOK_CACHE_PATH);
                    PreUtils.clearAll();
                    SharedPreUtils.getInstance().clearAll();
                    removeAllActivity();
                    Intent intent = new Intent(SettringActivity.this, LoginActivity.class);
                    startActivity(intent);*/
                }

                @Override
                public void onNoClick(Dialog dialog) {

                }
            }).show();
            MobclickAgent.onEvent(SettingActivity.this,UMConstant.SET_UP_ANDROID_ACCOUNT_ACCOUNT_QUIT_CLICK);
        } else if (v.getId() == R.id.cl_qinghuancun) {
            new MyDialog(SettingActivity.this, "你确定要清理缓存吗？", "确定", "取消", new MyDialog.DialogClickListener() {
                @Override
                public void onOkClick(Dialog dialog) {
                    FileUtils.deleteFile(Constant.BOOK_CACHE_PATH);
                    ReadSettingManager.getInstance().setTypefacePath(0, "");
                    tv_cache.setText("0 M");
                    showToast("清理完毕！");
                }

                @Override
                public void onNoClick(Dialog dialog) {

                }
            }).show();
            MobclickAgent.onEvent(SettingActivity.this,UMConstant.SET_UP_ANDROID_ACCOUNT_CLEAR_CACHE_CLICK);
        }

    }

    public void touristLogin() {
        post(Url.login, "", LoginResponse.class, new FFNetWorkCallBack<LoginResponse>() {
            @Override
            public void onSuccess(LoginResponse response) {
                if (response.getCode() == 200) {
                    FileUtils.deleteFile(Constant.BOOK_CACHE_PATH);
                    PreUtils.clearAll();
                    SharedPreUtils.getInstance().clearAll();
                    PreUtils.setBoolean("tourist", true);

                    // 保留Splash页面协议弹窗
                    PreUtils.setBoolean("XIE_YI", true);

                    //遊客模式下背景圖
                    int readbg = SharedPreUtils.getInstance().getInt(ReadSettingManager.SHARED_READ_BG, 0);
                    if (readbg < UserInfoBg.INSTANCE.getTHEME().length) {
                        SharedPreUtils.getInstance().putInt(ReadSettingManager.SHARED_READ_BG, ReadSettingManager.DEFAULT_READ_BG);
                    }
                    //遊客模式下用戶信息
                    PreUtils.setString("token", response.getData().getToken());
                    PreUtils.setString("Authorization", response.getData().getTokenHead() + response.getData().getToken());
                    PreUtils.setString("RefreshToken", response.getData().getRefreshToken());

                    /*removeAllActivity();
                    Intent intent = new Intent(SettringActivity.this, MainActivity.class);
                    intent.putExtra(Constants.TAB_CURRENT_ITEM, Constants.TAB_ITEM_MY);
                    startActivity(intent);*/
                    removeAboveActivity(MainActivity.class);
                }

            }

            @Override
            public boolean onFail(LoginResponse response) {
                Intent intent = new Intent();
                intent.setClass(SettingActivity.this,
                        LoginActivity.class);
                startActivity(intent);
                finish();
                return false;
            }
        }, "{\"loginType\":\"tourist_mode\",\"deviceId\":\"" + new PhoneInfoUtils(this).getIMEI() + "\",\"deviceName\":\"" + android.os.Build.MODEL + "\"}");
    }
}
